package com.zeny.coding.dto;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.zeny.coding.util.DtoUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.springframework.util.DigestUtils;

import javax.xml.bind.ValidationException;
import java.util.Date;
/**
 * <h2>会员dto</h2>
 **/
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
@SuppressWarnings("all")
public class MemberDto {

    private String id;

    private String mobile;

    private String password;

    private String name;

    private String photo;



    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
    private Date registerTime;


    /**
     * <h2>验证码</h2>
     **/
    private String imageCode;

    /**
     * <h2>图片验证码token</h2>
     **/
    private String imageCodeToken;

    private String token;

    private String smsCode;

    public void isValidForSave() throws ValidationException {
        //检查手机
        DtoUtil.valid(this.mobile, true, 1, 30);
        //检查密码
        DtoUtil.valid(this.password, true, 1, 50);
        //检查名字
        DtoUtil.valid(this.name, true, 1, 50);
        //检查短信验证码/邮箱验证码
        DtoUtil.valid(this.smsCode, true, 1, 10);

        //二次加密密码
        this.password = DigestUtils.md5DigestAsHex(this.password.getBytes());
        this.id = null;
    }

    public void isValidForUpdate() {
    }

    public void validForLogin() throws ValidationException {
        DtoUtil.valid(this.password, true, 1, 50);
        DtoUtil.valid(this.imageCodeToken, true, 1, 50);
        DtoUtil.valid(this.imageCode, true, 1, 50);
        DtoUtil.valid(this.mobile, true, 1, 30);
        //密码二次加密
        this.password = DigestUtils.md5DigestAsHex(this.password.getBytes());
    }

    public void validForResetPassword() throws ValidationException {
        DtoUtil.valid(this.password, true, 1, 50);
        DtoUtil.valid(this.mobile, true, 1, 30);
        DtoUtil.valid(this.smsCode, true, 1, 10);
        //密码二次加密
        this.password = DigestUtils.md5DigestAsHex(this.password.getBytes());
    }
}